#sql 封装
import  pymysql
class SqlContection():
    def __init__(self,host='192.168.7.160',
                 port=3306,user='root',
                 passwd='sq',
                 database='sq-waimai'):
        self.db = pymysql.connect(host=host,
                                  port=port,
                                  user=user,
                                  passwd=passwd,
                                  database=database)
        self.cursor = self.db.cursor()

    def selects(self,sql,many):
        self.cursor.execute(sql)
        if many == True:
            result = self.cursor.fetchall()
        else:
            result = self.cursor.fetchone()
        return result

    def do(self,sql):   #剩下的操作皆如此
        self.cursor.execute(sql)
        try:
            self.db.commit()
        except Exception as error:
            self.db.rollback()
            print('MySQL的错误：',f'{error}')

    def add(self,sql):
        self.do(sql)

    def xiugai(self,sql):
        self.do(sql)

    def deletes(self,sql):
        self.do(sql)

    def exit(self):
        self.cursor.close()
        self.db.close()


if __name__ == '__main__':
    dbs = SqlContection()
    # select = dbs.selects('select * from t_cms_channel',many=True)
    adds = dbs.add('insert into t_cms_channel(code,name) values("name","你的名字")')


